<template>
  <div class="home">
    <custom-tree
      class="custom-tree"
      :search-method="searchMethod"
      :remote-method="remoteMethod"
      :lazy-method="lazyMethod"
    ></custom-tree>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import CustomTree from '@/components/Tree/sddl-tree.vue'

@Component({
  components: {
    CustomTree
  }
})
export default class Home extends Vue {

  private searchMethod() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve([
          { id: '1-2-1', name: '节点1-2-1' }
        ])
      }, 1000)
    })
  }

  private remoteMethod(id: string) {
    console.log(id)
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve({
          label: '节点1', // 最上层节点
          id: '1',
          children: [
            { label: '节点1-1', id: '1-1', children: [] },
            { label: '节点1-2', id: '1-2', children: [
              {id: '1-2-1', label: '节点1-2-1'}
            ] } // 搜索对应id的节点
          ]
        })
      }, 1000)
    })
  }

  private async lazyMethod(id: string) {
    let status = 'ok'
    if (id === '2') {
      status = 'fial'
    }
    await new Promise((resolve, reject) => {
      setTimeout(() => { resolve({}) }, 1000)
    })
    if (status === 'fial') {
      return Promise.reject({})
    }
  
    return Promise.resolve([
      { id: '1', label: '节点1', children: [] },
      { id: '2', label: '节点2', children: [] },
      { id: '3', label: '节点3', children: [] }
    ])
  }
}
</script>

<style lang="scss">
.custom-tree {
  .el-scrollbar {
    height: 400px;
    .el-scrollbar__wrap {
      overflow: auto;
      max-height: 100%;
    }
    .el-scrollbar__view {
      height: 100%;
    }
  }
}
</style>
